package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.StickerAdapter;
import flc.ast.bean.MyStickerBean;
import flc.ast.databinding.ActivityPicStickerBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

public class PicStickerActivity extends BaseAc<ActivityPicStickerBinding> {

    public static String imgPath = "";
    private StickerAdapter stickerAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_sticker;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPicStickerBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivPicStickerSave.setOnClickListener(this);
        mDataBinding.ivNoSticker.setOnClickListener(this);
        mDataBinding.rvStickerList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL,false));
        stickerAdapter = new StickerAdapter();
        mDataBinding.rvStickerList.setAdapter(stickerAdapter);
        stickerAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivNoSticker://无贴纸
                mDataBinding.ivStickerView.clear();
                break;
            case R.id.ivPicStickerSave://保存
                if (mDataBinding.ivStickerView.hasSticker()) {
                    mDataBinding.ivStickerView.setShowHelpToolFlag(false);
                    saveImg();
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        Resources res = getResources();
        Bitmap bmp = BitmapFactory.decodeResource(res, stickerAdapter.getItem(position).getImg());
        mDataBinding.ivStickerView.addBitImage(bmp);
    }

    @Override
    protected void initData() {
        //获取图片
        if (imgPath == null || TextUtils.isEmpty(imgPath)) return;
        Glide.with(this).load(imgPath).into(mDataBinding.ivPicStickerImg);
        mDataBinding.ivPicStickerImg.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mDataBinding.ivPicStickerImg.setScaleEnabled(false);// 禁用缩放
        //贴纸数据
        List<MyStickerBean> listSticker = new ArrayList<>();
        listSticker.add(new MyStickerBean(R.drawable.iv_tz1, false));
        listSticker.add(new MyStickerBean(R.drawable.iv_tz2, false));
        listSticker.add(new MyStickerBean(R.drawable.iv_tz3, false));
        listSticker.add(new MyStickerBean(R.drawable.iv_tz4, false));
        listSticker.add(new MyStickerBean(R.drawable.iv_tz5, false));
        listSticker.add(new MyStickerBean(R.drawable.iv_tz6, false));
        listSticker.add(new MyStickerBean(R.drawable.iv_tz7, false));
        listSticker.add(new MyStickerBean(R.drawable.iv_tz8, false));
        listSticker.add(new MyStickerBean(R.drawable.iv_tz9, false));
        listSticker.add(new MyStickerBean(R.drawable.iv_tz10, false));
        listSticker.add(new MyStickerBean(R.drawable.iv_tz11, false));
        listSticker.add(new MyStickerBean(R.drawable.iv_tz12, false));
        stickerAdapter.setList(listSticker);
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.save_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.rlSaveImg);
                        //保存到相册
                        File file = ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        emitter.onNext(file.getPath());
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_suc);
                        PicPreviewActivity.imgPath = path;
                        startActivity(PicPreviewActivity.class);
                    }
                });
            }
        }, 1000);
    }

}